package com.javaking.clanteam.hvz.utils;

import org.json.JSONException;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.javaking.clanteam.hvz.sql.HVZUser;
import com.javaking.clanteam.hvz.sql.SQLRelay;

public class UserActionActivity extends Activity {
	
	public void finish(String title, String message, int resultCode) {
		Intent resultIntent = new Intent();
		Bundle extras = new Bundle();
		extras.putString("title", title);
		extras.putString("message", message);
		extras.putInt("resultCode", resultCode);
		resultIntent.putExtras(extras);
		setResult(resultCode, resultIntent);
		
		finish();
	}
	
	private static final int SCORE_KILLCODE = 0;
	private HVZUser user;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle extras = getIntent().getExtras();
        String action = extras.getString("action");
        if (action==null)
        {
        	// TODO: Add code for handling no action
        	return;
        }
        
        try {
			user = new HVZUser(extras.getString("HVZUser_string"));
		} catch (JSONException e) {
			e.printStackTrace();
			finish("ERROR",e.getMessage(),RESULT_CANCELED);
		}
        
        setupContent(action);
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    	Bundle extras = data.getExtras();
    	switch (requestCode)
    	{
    	case SCORE_KILLCODE:
    		String killcode = extras.getString("barcode");
			SQLRelay.assign_killcode(user, killcode);
			finish("Success!",String.format(
					"Successfully added killcode:%s",killcode),RESULT_OK);
    		break;
    	}
    	
    	
    }
    

	private void setupContent(String action) {
		
		// Standard player actions.
		if (action.equals("Score Killcode")) {
			setupScore();
		} else if(action.equals("Change Username")) {
			setupChangeUsername();
		} else if(action.equals("Delete User")) {
			setupDeleteUser();
		} else {
			// TODO: :'(
		}
	}

	public void performAction(View v)
	{
		
	}

	private void setupChangeUsername() {
		PromptDialog dialog = new PromptDialog(this,"Enter new username", null) {
			
			@Override
			public boolean onOkClicked(String input) {
				String oldUsername = user.getUsername();
				user.setUsername(input);
				SQLRelay.modifyUser(user);
				finish("Success!",String.format(
						"Successfully changed username from " +
						"\"%s\" to \"%s\"",oldUsername,
						user.getUsername()),RESULT_OK);
				return true;
			}
		};
		
		dialog.show();
	}

	private void setupScore() {
		Intent scanIntent = new Intent(this,BarcodeScanActivity.class);
		startActivityForResult(scanIntent, SCORE_KILLCODE);
	}

	private void setupDeleteUser() {
		
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle("User Delete");
		builder.setMessage("Are you sure you want to delete this user?");
		
		builder.setPositiveButton(android.R.string.yes,
				new DialogInterface.OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {
				SQLRelay.removeUser(user);
				finish("Success!",String.format(
						"Successfully deleted %s",user.getUsername()),
						RESULT_OK);
			}
		});
		
		builder.setPositiveButton(android.R.string.no,
				new DialogInterface.OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		
		builder.show();
	}
	
}
